using Godot;
using GoGameClient.script.global;
using GoGameClient.script.tool;

namespace GoGameClient.script.node.load;

public partial class Load : Control
{
    // Called when the node enters the scene tree for the first time.
    private SocketControl _socketControl;
    private ResourcesControl _resourcesControl;
    private string _scenePath = "res://scene/Caption.tscn";
    public override void _Ready()
    {
        ResourceLoader.LoadThreadedRequest(_scenePath);
        _socketControl = SocketControl.Instance();
        _socketControl.SetUrl(Config.GetIp(), Config.GetPort(), Config.GetContext());
        _resourcesControl = ResourcesControl.Instance();
        var progressBar = GetNode<TextureProgressBar>("LoadProgressBar");
        _resourcesControl.InitMapinfo((count) =>
        {
            progressBar.Value = count;
            if (count < 80)
                ResourceLoader.LoadThreadedRequest(_scenePath);
        });
    }

    public override void _Process(double delta)
    {
        if (ResourceLoader.LoadThreadedGetStatus(_scenePath) != ResourceLoader.ThreadLoadStatus.Loaded)
        {
            return;
        }
        GetTree().ChangeSceneToPacked((PackedScene)ResourceLoader.LoadThreadedGet(_scenePath));
    }
}